﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.Utils;
using DevExpress.Data.Filtering;
using System.Reflection;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;

namespace QuanLyThuNgan
{
    public partial class frm_DM_LoaiDichVu : DevExpress.XtraEditors.XtraForm
    {
        public frm_DM_LoaiDichVu()
        {
            InitializeComponent();
        }

        string sTenSearch = "";
        string sColSearch;
        string sColRow;

        public static string sIDSearch = "";
        public static string sTenValueSearch = "";
        string sTenTam, sColTam;

        DataTable dtkt;
        DMLoaiDichVuCTL loaiDVCtl = new DMLoaiDichVuCTL();
        DMLoaiDichVuDTO loaiDVDto = new DMLoaiDichVuDTO();
        DataTable dtLoaiDV = new DataTable();
        void m_DataKhoiTao()
        {
            dtkt = new DataTable();
            dtkt.Columns.Add("loaidvu", typeof(string));
            dtkt.Columns.Add("TenLoaiDVu", typeof(string));
            dtkt.Columns.Add("tendv", typeof(string));
            dtkt.Columns.Add("guidi", typeof(bool));

        }

        //load du lieu len form-------------------------------------------
        private void frm_DM_LoaiDichVu_Load(object sender, EventArgs e)
        {
            //load madonvi----------------------------------
            DataTable dtdv = loaiDVCtl.m_SelectAllDonVi();
            m_LoadLookupEditDonVi(dtdv, GridcomboDonVi, "TenDonVi", "MaDonVi");
            //GridcomboDonVi.View.Columns["MaDonVi"].Visible = false;
            //load data-------------------------------
            dtLoaiDV = loaiDVCtl.m_SelectAllDMLoaiDV();
            gridControlLoaiDVu.DataSource = dtLoaiDV;

            //set condition button-----------------------
            gridViewLoaiDVu.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            gridViewLoaiDVu.OptionsBehavior.ReadOnly = true;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            btnSave.Enabled = false;

        }

        //load don vi combobox in grid----------------------------------------
        void m_LoadLookupEditDonVi(DataTable _dt, DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit _lo, string strTen, string strID)
        {
            //foreach (DataRow dr in _dt.Rows)
            //{
            //    dr[strTen] = Uit.it_Font.VNIToUnicode(dr[strTen].ToString());
            //}
            _lo.DataSource = _dt;
            _lo.DisplayMember = strTen;
            _lo.ValueMember = strID;
        }

        //luu thong tin du lieu----------------------------------------
        private void btnSave_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)gridControlLoaiDVu.DataSource;

            if (dt != null)
            {
                    for (int i = 0; i < gridViewLoaiDVu.RowCount; i++)
                    {
                        DataRow dr = gridViewLoaiDVu.GetDataRow(i);
                        gridViewLoaiDVu.MoveNext();
                        gridViewLoaiDVu.MovePrev();
                        if (dr["loaidvu"] != null)
                        {
                            loaiDVDto.SLoaidvu = dr["loaidvu"].ToString().Trim();
                            loaiDVDto.STenloai = Uit.it_Font.UnicodeToVNI(dr["TenLoaiDVu"].ToString().Trim());
                            loaiDVDto.SMadv = dr["madv"].ToString().Trim();
                            loaiDVDto.BGuiDi = Uit.it_Parse.ToBoolean(dr["guidi"].ToString());
                            loaiDVDto.STenLoaiDichVu = dr["TenLoaiDVu"].ToString().Trim();
                            //insert and update database------------------------
                            if (dr["IDHoTro"].ToString() != "1")
                            {
                                loaiDVCtl.m_InsertDMLoaiDV(loaiDVDto);
                            }
                            else
                            {
                                loaiDVDto.SLoaidvu = dr["loaidvu"].ToString().Trim();
                                loaiDVCtl.m_UpdateDMLoaiDV(loaiDVDto);
                            }
                            
                        }
                    }
                }
                
                XtraMessageBox.Show("Lưu thành công");
                DataTable dtloaddv = loaiDVCtl.m_SelectAllDMLoaiDV();
                gridControlLoaiDVu.DataSource = dtloaddv;

            //set dieu kien tren gridview--------------------------------
                btnSave.Enabled = false;
                btnUpdate.Enabled = true;
                btnInsert.Enabled = true;
                gridViewLoaiDVu.OptionsBehavior.ReadOnly = true;
                gridViewLoaiDVu.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;

        }

      //set error in grid-----------------------------------------------------------------------------------
        private void gridViewLoaiDVu_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            GridView view = gridViewLoaiDVu;
            GridColumn colLoaiDV = view.Columns["loaidvu"];
            GridColumn colTenLoaiDV = view.Columns["TenLoaiDVu"];


            if (sColRow != "guidi")
            {
                    string sLoaiDV = view.GetRowCellValue(e.RowHandle, colLoaiDV).ToString().ToUpper().Trim();
                    string sTenLoaiDV = view.GetRowCellValue(e.RowHandle, colTenLoaiDV).ToString().ToUpper().Trim();
                    //lay gia tri tai row---------------
                    int _iRowIndex = e.RowHandle;

                    //kiem tra gia tri trong trong cellcolumn-------------------
                    if (sLoaiDV == "")
                    {
                        e.Valid = false;
                        view.SetColumnError(colLoaiDV, "Loại dịch vụ không được trống");
                    }
                    if (sTenLoaiDV == "")
                    {
                        e.Valid = false;
                        view.SetColumnError(colTenLoaiDV, "Tên loại dịch vụ không được trống");

                    }

                    //chay tu 0 -> n
                    for (int i = 0; i < gridViewLoaiDVu.RowCount; i++)
                    {
                        //neu dong dang sua khac voi dong duyet
                        if (i != _iRowIndex)
                        {
                            //Lay dong dang duyet
                            DataRow drldv = gridViewLoaiDVu.GetDataRow(i);
                            string sTestLoaiDV = drldv["loaidvu"].ToString().ToUpper().Trim();
                            string sTestTenLoaiDV = drldv["TenLoaiDVu"].ToString().ToUpper().Trim();

                            //So sanh gia tri dang sua voi dong duyet 
                            //neu trung loai dich vu
                            if (sLoaiDV == sTestLoaiDV)
                            {
                                e.Valid = false;
                                XtraMessageBox.Show("Loại dịch vụ này đã tồn tại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                view.SetColumnError(colLoaiDV, " Loại dịch vụ không được trùng");
                                return;
                            }
                            else
                            {
                                view.SetColumnError(colLoaiDV, "");
                            }
                            //neu trung ten dich vu
                            if (sTenLoaiDV == sTestTenLoaiDV)
                            {
                                e.Valid = false;
                                XtraMessageBox.Show(" Tên loại dịch vụ này đã tồn tại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                view.SetColumnError(colTenLoaiDV, " Tên Loại dịch vụ không được trùng");
                                return;
                            }
                            else
                            {
                                view.SetColumnError(colTenLoaiDV, "");
                            }
                        }
                    }
                }
            btnSave.Enabled = true;
        }

        //thoat chuong trinh-----------------------------------
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //xoa du lieu tren row-----------------------------------
        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataRow dr = gridViewLoaiDVu.GetDataRow(gridViewLoaiDVu.FocusedRowHandle);
            if (dr != null)
            {
                loaiDVDto.SLoaidvu = dr["loaidvu"].ToString();
                loaiDVCtl.m_DeleteDMLoaiDV(loaiDVDto);
              
            }
            XtraMessageBox.Show("Xóa thành công");
            DataTable dtloaddv = loaiDVCtl.m_SelectAllDMLoaiDV();
            gridControlLoaiDVu.DataSource = dtloaddv;
            //reset load du lieu----------------------
            gridViewLoaiDVu.ActiveFilterEnabled = false;
            gridViewLoaiDVu.ClearColumnsFilter();
            gridViewLoaiDVu.OptionsFilter.Reset();
        }

        //tao danh muc moi---------------------------------------
        private void btnInsert_Click(object sender, EventArgs e)
        {
            gridViewLoaiDVu.OptionsBehavior.ReadOnly = false;
            btnSave.Enabled = false;
            btnInsert.Enabled = false;
            btnUpdate.Enabled = false;
            gridViewLoaiDVu.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            //reset load du lieu----------------------
            gridViewLoaiDVu.ActiveFilterEnabled = false;
            gridViewLoaiDVu.ClearColumnsFilter();
            gridViewLoaiDVu.OptionsFilter.Reset();
        }

        //sua du lieu--------------------------------------------
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            gridViewLoaiDVu.OptionsBehavior.ReadOnly = false;
            btnSave.Enabled = true;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = false;
        }

        //bo qua du lieu dang nhap-------------------------------
        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnInsert.Enabled = true;
            btnSave.Enabled = false;
            gridViewLoaiDVu.OptionsBehavior.ReadOnly = true;
            DataTable dtloaddv = loaiDVCtl.m_SelectAllDMLoaiDV();
            gridControlLoaiDVu.DataSource = dtloaddv;
            gridViewLoaiDVu.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            //reset load du lieu----------------------
            gridViewLoaiDVu.ActiveFilterEnabled = false;
            gridViewLoaiDVu.ClearColumnsFilter();
            gridViewLoaiDVu.OptionsFilter.Reset();
        }

        private void gridViewLoaiDVu_FocusedColumnChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedColumnChangedEventArgs e)
        {
            sColRow = e.FocusedColumn.FieldName;
        }

        //tim ten can search---------------------------------------------
        private void gridViewLoaiDVu_CellValueChanging(object sender, CellValueChangedEventArgs e)
        {
            if (gridViewLoaiDVu.EditingValue != null)
            {
                sTenSearch = e.Value.ToString();
                sColSearch = e.Column.FieldName;
            }
            
        }

        //set ten can tim----------------------------------------------------------------
        private void gridViewLoaiDVu_ColumnFilterChanged(object sender, EventArgs e)
        {
            ColumnView view = gridViewLoaiDVu;
            view.ActiveFilter.Add(view.Columns[sColSearch], new ColumnFilterInfo("[" + sColSearch + "] Like '%" + sTenSearch + "%'", ""));
        
        }

        //show list data in gridlookupedit in gridcontrol------------
        private void GridcomboDonVi_Enter(object sender, EventArgs e)
        {
            SendKeys.Send("{F4}");
        }

        private void GridcomboDonVi_EditValueChanged(object sender, EventArgs e)
        {

        }

    }
}